package com.cmcc.aregister.ui.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import com.cmcc.aregister.cs.model.NoticeModel;
import com.cmcc.aregister.ui.adapter.NoticeAdapter;

public class NoticeActivity extends ParentActivity
{

    private Button top_left_image;
    private TextView titleView;
    private NoticeAdapter adapter;
    private ListView listView;
    private int jump_type = Arguments.NOTICE_JUMP_TYPE_NOTIFICATION;// 默认是从通知处来到该界面，返回到首页
    private List<NoticeModel> notices;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        init(R.layout.p_notice);
        mHttpManager.setCxt(getParent());
    }

    @Override
    protected void setupData()
    {
        jump_type = getIntent().getIntExtra(Arguments.NOTICE_JUMP_TYPE,
            Arguments.NOTICE_JUMP_TYPE_NOTIFICATION);
    }

    private void setListView()
    {
        adapter = new NoticeAdapter(this, notices);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        getNotice();
    }

    @Override
    protected void setupView()
    {
        top_left_image = (Button) findViewById(R.id.top_left_image);
        top_left_image.setVisibility(View.VISIBLE);
        top_left_image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                gotoHome();
            }
        });
        ((ImageView) findViewById(R.id.title))
            .setBackgroundResource(R.drawable.notice);

        listView = (ListView) findViewById(R.id.listview);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                int position, long arg3)
            {
                gotoDetail(adapter.list.get(position));
            }

        });
    }

    @Override
    public void onBackPressed()
    {
        if (jump_type == Arguments.NOTICE_JUMP_TYPE_MORE)
        {
            super.onBackPressed();
        }
        else
        {
            gotoHome();
        }

    }

    private void gotoHome()
    {
        Intent intent = new Intent(this, NewIndexActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    private void gotoDetail(NoticeModel notice)
    {
        Intent intent = new Intent(this, NoticeDetailActivity.class);
        intent.putExtra(Arguments.NOTICE_MODEL, notice);
        intent.putExtra(Arguments.NOTICE_JUMP_TYPE, jump_type);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private void getNotice()
    {
        showProgressDialog();
        getNoticeTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_NOTICE, Arguments.GET_NOTICE_URL,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        getNoticeTask.setUIHandler(getNoticeTaskHandler);
        mHttpManager.execute(getNoticeTask);
    }

    private HttpAsyncTask getNoticeTask;
    Handler getNoticeTaskHandler = new Handler() {
        public void handleMessage(Message msg)
        {
            dismissProgressDialog();
            switch (msg.what)
            {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                if (msg.obj != null)
                {
                    Map<String, Object> map = (Map<String, Object>) msg.obj;
                    notices = (ArrayList<NoticeModel>) map
                        .get(Arguments.XML_NOTICE_LIST);
                    if (notices != null && notices.size() > 0)
                    {
                        setListView();
                    }
                }
                break;
            }
        };
    };
}
